package com.item.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.item.annocation.SysLog;
import com.item.entity.WxUser;
import com.item.entity.vo.WxOrderVo;
import com.item.result.ResultVo;
import com.item.service.WxOrderDetailService;
import com.item.service.WxOrderService;
import com.item.util.EasyExcelUtils;
import com.item.util.JSONUtil;
import com.item.util.ResultVoUtil;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/order")
public class WxOrderController {

    private final static String FILE_NAME = "订单列表";
    private final static String EMPTY_FILE = "无数据";
    private static String goodsString;

    static {
        goodsString = "{\n" +
                "    \"order\":{\n" +
                "        \"order_list\":[\n" +  //订单详情
                "            {\n" +
                "                \"card_id\":\"213fda5646a3d\",\n" +
                "                \"face_price\":\"120\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"card_id\":\"111fda5646a55\",\n" + //卡id
                "                \"face_price\":\"300\"\n" +  // 卡面值
                "            }\n" +
                "        ],\n" +
                "        \"open_id\":\"95159\",\n" +  //  付款人id
                "        \"realpay_amount\":\"500\",\n" + //支付金额
                "        \"pay_time\":\"2020-7-23 10:15:05\"\n" +   //支付时间
                "    }\n" +
                "}";
    }

    @Autowired
    private WxOrderService wxOrderService;
    @Autowired
    private WxOrderDetailService detailService;

    /**
     * 模拟下单接口
     *
     * @param goods 订单信息
     * @return
     */
    @Transactional
    @PostMapping("/pay")
    @SysLog
    @ResponseBody
    public ResultVo createOrder(@RequestBody(required = false) JSONObject goods) {

        if (goods == null) {
            goods = JSONUtil.toObject(goodsString, JSONObject.class);
        }
        JSONObject order = goods.getJSONObject("order");

        JSONObject orderJsonObject = new JSONObject();
        orderJsonObject.put("open_id", order.get("open_id"));
        orderJsonObject.put("realpay_amount", order.get("realpay_amount"));
        orderJsonObject.put("pay_time", order.get("pay_time"));

        ResultVo orderResult = wxOrderService.addOrder(orderJsonObject);

        if (400 == orderResult.getCode()) {
            return ResultVoUtil.error();
        }
        String orderNo = (String) orderResult.getData();
        JSONArray orderList = order.getJSONArray("order_list");
        MDC.putCloseable("get_info", orderNo);
        return detailService.addOrderDetail(orderNo, orderList);
    }

    /**
     * 获取list
     * @param page
     * @param limit
     * @param orderTimeFrom
     * @param orderTimeTo
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultVo list(@RequestParam(required = false,defaultValue = "1") Integer page,
                         @RequestParam(required = false,defaultValue = "20") Integer limit,
                         @RequestParam(required = false) String orderTimeFrom,
                         @RequestParam(required = false) String orderTimeTo){
        HashMap<String, Object> params = Maps.newHashMap();
        params.put("page",page);
        params.put("limit",limit);
        params.put("orderTimeFrom",orderTimeFrom);
        params.put("orderTimeTo",orderTimeTo);
        //service调用
        ResultVo pageList = wxOrderService.getPageList(params);
        System.out.println(pageList);
        return pageList;
    }

    /**
     * excel导出
     * @param page
     * @param limit
     * @param orderTimeFrom
     * @param orderTimeTo
     * @param response
     * @return
     */
    @RequestMapping("/export")
    public void export(@RequestParam(required = false,defaultValue = "1") Integer page,
                         @RequestParam(required = false,defaultValue = "20") Integer limit,
                         @RequestParam(required = false) String orderTimeFrom,
                         @RequestParam(required = false) String orderTimeTo,
                        HttpServletResponse response){
            try {
                HashMap<String, Object> params = Maps.newHashMap();
                params.put("page",page);
                params.put("limit",limit);
                params.put("orderTimeFrom",orderTimeFrom);
                params.put("orderTimeTo",orderTimeTo);
                //service调用
                List<WxOrderVo> list = wxOrderService.downList(params);
                if(list.isEmpty()) {
                    WxOrderVo vo = new WxOrderVo();
                    vo.setOrderNo(EMPTY_FILE);
                    list.add(new WxOrderVo());
                }
                EasyExcelUtils.webWriteExcel(response, list, WxOrderVo.class, FILE_NAME,false);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }


    @GetMapping("/get")
    @ResponseBody
    public ResultVo getOrderById(String orderNo) {
        return wxOrderService.getOrderById(orderNo);
    }

    @PostMapping("/test")
    //@SysLog
    @ResponseBody
    public ResultVo testAnno(WxUser goods) {

        System.out.println("========执行service方法========");
        return ResultVoUtil.success(goods);
    }
}

